package com.shelpe.services.simbasync.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.shelpe.services.simbasync.model.mongo.SyncPerDayRecord;
import com.shelpe.services.simbasync.mongorepository.SyncPerDayRecordRepository;
import com.shelpe.services.simbasync.service.SyncRecordService;

@Service
public class SyncRecordServiceImpl implements SyncRecordService {

	@Autowired
	private SyncPerDayRecordRepository syncRep;
	
	@Override
	public void deleteAllSyncRecord() {
		// TODO Auto-generated method stub
		syncRep.deleteAll();
	}

	@Override
	public SyncPerDayRecord getSyncPerDayRecordById(String processName) {
		// TODO Auto-generated method stub
		return syncRep.findOne(processName);
	}

	@Override
	public void saveSyncRecord(SyncPerDayRecord record) {
		// TODO Auto-generated method stub
		syncRep.save(record);
	}
	
	@Override
	public void updateSyncRecordByProcess(String processName, int num) {
		
		SyncPerDayRecord syncRecord = getSyncPerDayRecordById(processName);
		if(syncRecord == null){
			syncRecord = new SyncPerDayRecord();
			syncRecord.setProcessName(processName);
			syncRecord.setMessage("has no effect data");
		}
		Integer failedNum = syncRecord.getFailedNum();
		if(failedNum != null){
			failedNum += num;
		}else{
			failedNum = num;
		}
		syncRecord.setFailedNum(failedNum);
		saveSyncRecord(syncRecord);
	}

}
